Page({
  data: {
    newsList: [],
    pageIndex: 1,
    loading: false,
    noMore: false
  },
  onLoad: function () {
    this.getNotify();
  },
  onPullDownRefresh: function () {
    this.setData({
      pageIndex: 1,
      noMore: false,
      loading: true
    }, () => {
      this.getNotify();
      wx.stopPullDownRefresh();
    });
  },
  onPageScroll: function (e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },
  onReachBottom: function () {
    if (this.data.noMore) return;
    this.setData({
      pageIndex: this.data.pageIndex + 1,
      loading: true
    }, () => {
      this.getNotify();
    });
  },
  getNotify: function () {
    let page = this.data.pageIndex;
    wx.util.request('common.notify.list', {
      query: `page=${page}`
    }).then(res => {
      let list = (page == 1 ? [] : this.data.newsList);
      for (let item of res) {
        item.date = wx.time.toDate(item.created_at);
        list.push(item);
      }
      this.setData({
        newsList: list,
        loading: false,
        noMore: res.length < 10
      });
      setTimeout(() => {
        let component = this.selectComponent('#rf-tips-ctx');
        if (component) {
          component.showTips({
            msg: `刷新成功，为你更新了${res.length}条数据`,
            duration: 2000,
            type: 'translucent'
          });
        }
      }, 300);
    });
  },
  detail: function (e) {
    wx.navigateTo({
      url: `detail?id=${e.currentTarget.dataset.id}`,
    })
  }
})